package com.dq.controller.op.opmerchant;

import com.dq.exception.customException.AdminException;
import com.dq.controller.in.OrgOrderQuery;
import com.dq.controller.op.opmerchant.in.query.OrderQuery;
import com.dq.vo.ListVo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.orders.OrgOrder;
import com.dq.domain.orders.UserOrder;
import com.dq.domain.org.Org;
import com.dq.service.AllService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author hx
 * @version 1.0
 * @date 2019/9/19 17:06
 */
@Api(description = "后台财务模块")
@RestController
@RequestMapping("/sys/finance")
public class FinanceController extends AllService {


    @ApiOperation("条件查询门店采购单")
    @PostMapping("/queryShopBuy")
    @RequiresPermissions("sys:finance:queryShopBuy")
    public ListVo<OrgOrder.Vo> queryList(@RequestBody OrgOrderQuery orgOrderQuery) throws AdminException {
        Map<String, Object> query = orgOrderQuery.parseMap();
        List<OrgOrder> orgOrderList = dao.find(dao.queryAdminHql(OrgOrder.class, query), query);
        Long total = dao.count(dao.countAdminHql(OrgOrder.class, query), query);

        return new ListVo((List<OrgOrder.Vo>) VO.toVOCollection(orgOrderList), orgOrderList.size());
    }

    @ApiOperation("条件查询门店/平台销售单")
    @PostMapping("/queryBuy")
    @RequiresPermissions("sys:finance:queryBuy")
    public ListVo<UserOrder.Vo> queryBuy(@RequestBody OrderQuery userOrderQuery) throws AdminException {
        Map<String, Object> query = userOrderQuery.parseMap();
        List<UserOrder> orgOrderList = dao.find(dao.queryAdminHql(UserOrder.class, query), query);
        Long total = dao.count(dao.countAdminHql(UserOrder.class, query), query);

        return new ListVo((List<UserOrder.Vo>) VO.toVOCollection(orgOrderList), orgOrderList.size());
    }


    @ApiOperation("平台销售列表")
    @GetMapping("/platformSaleList")
    @RequiresPermissions("sys:finance:platformSaleList")
    public ListVo<UserOrder.Vo> findPlatformList() throws AdminException {
        List<UserOrder> userOrderList = userOrderRepository.findByOrg(dao.get(Org.class,shiroService.getDeptId()));
        return new ListVo((List<UserOrder.Vo>) VO.toVOCollection(userOrderList), userOrderList.size());

    }

    @ApiOperation("门店销售列表")
    @PostMapping("/shopSaleList")
    @RequiresPermissions("sys:finance:shopSaleList")
    public ListVo<UserOrder.Vo> findShopSaleList() throws AdminException {
        List<UserOrder> userOrderList = userOrderRepository.findByOrg(dao.get(Org.class,shiroService.getDeptId()));
        return new ListVo((List<UserOrder.Vo>) VO.toVOCollection(userOrderList), userOrderList.size());
    }
}
